package dom4j;

import org.dom4j.*;
import org.dom4j.Attribute;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Class description here
 * Created at 2015/7/23 21:28
 *
 * @author young
 * @version 1.0.0
 */
public class DocParse {
	/**
	 * 文本转XML对象
	 * */
	public static Xml toXml(String content){
		try {
			Document doc = DocumentHelper.parseText(content);
			Element element = doc.getRootElement();
			return fillXml(element);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 递归添加XML对象
	 * */
	private static Xml fillXml(Element element){
		Xml xml = new Xml();
		xml.setName(element.getName());
		if(element.elements().size() > 0){
			List<Xml> children = new ArrayList<Xml>();
			List<Element> elements = element.elements();
			for(Element item : elements){
				children.add(fillXml(item));
			}
			xml.setChildren(children);
		}else{
			xml.setValue(element.getText());
		}
		List<Attribute> attributes = element.attributes();
		if(attributes.size() > 0){
			List<Attr> attrs = new ArrayList<Attr>();
			for (Attribute attribute : attributes){
				Attr attr = new Attr();
				attr.setName(attribute.getName());
				attr.setValue(attribute.getValue());
				attrs.add(attr);
			}
			xml.setAttrs(attrs);
		}
		return xml;
	}

	/**
	 * XML转文本
	 * @param xml XML对象
	 * @param charset 文本字符集编码
	 * @param prettyText 是否格式化输出文本
	 * */
	public static String toText(Xml xml, String charset, boolean prettyText){
		Document doc = DocumentHelper.createDocument();
		fillElement(doc, xml, true);
		if(prettyText){
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding(charset);
			StringWriter writer = new StringWriter();
			try {
				XMLWriter xmlWriter = new XMLWriter(writer, format);
				xmlWriter.write(doc);
				xmlWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return writer.toString();
		}else{
			doc.setXMLEncoding(charset);
			return doc.asXML();
		}
	}

	/**
	 * XML转文本
	 * @param xml XML对象
	 * @param prettyText 是否格式化输出文本
	 * */
	public static String toText(Xml xml, boolean prettyText){
		return toText(xml, "UTF-8", prettyText);
	}

	public static String toText(Xml xml){
		return toText(xml, false);
	}

	private static void fillElement(Object object, Xml xml, boolean root){
		Element element1;
		if(root){
			Document element = (Document)object;
			element1 = element.addElement(xml.getName());
		}else{
			Element element = (Element) object;
			element1 = element.addElement(xml.getName());
		}
		if(xml.getAttrSize() > 0){
			for(Attr attr : xml.getAttrs()){
				element1.addAttribute(attr.getName(), attr.getValue());
			}
		}
		if(xml.hasChild()){
			for(Xml child : xml.getChildren()){
				fillElement(element1, child, false);
			}
		}else{
			if(null == xml.getValue()){
				element1.addText("");
			}else{
				element1.addText(xml.getValue());
			}
		}
	}

}
